package com.winway.sixbook.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.winway.sixbook.HomeActivity;
import com.winway.sixbook.R;
import com.winway.sixbook.bean.SeedData;
import com.winway.sixbook.custom.CloseActivityClass;
import com.winway.sixbook.service.SeedManager;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
 
public class GridViewChooose extends Activity {
    
	private static final String TAG = "GridViewChooose";
	private SeedManager mseedManager;
	
	private ImageButton top_tbn_fanhui;
	private Button top_tbn_xinzeng;
	private TextView title;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view_chooose);
        CloseActivityClass.activityList.add(this);
        
        top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
        title = (TextView)findViewById(R.id.title);
        
        top_tbn_fanhui.setOnClickListener(mListener);
        top_tbn_xinzeng.setVisibility(View.INVISIBLE);
        title.setText("种子类型选择");
        
        if (mseedManager == null) {
			mseedManager = new SeedManager(this);
			mseedManager.openDataBase();
        }
        
        GridView gridview = (GridView) findViewById(R.id.GridView);
        List<SeedData> seedlistDatas = mseedManager.fetchAllSeedList();
        ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>();
        
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("ItemImage", R.drawable.zui_hao_normal);
        map1.put("ItemText", "最好");
        map1.put("ItemTypeid", "0");
        meumList.add(map1);
        
        if(null != seedlistDatas){
	        for (int i = 0; i < seedlistDatas.size(); i++) {
	        	SeedData seedData= seedlistDatas.get(i);
	        	if(null != seedData){
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("ItemImage", R.drawable.ask_more_normal);
                    map.put("ItemText", seedData.getSeedName());
                    map.put("ItemTypeid", seedData.getId());
                    meumList.add(map);
	        	}
			}
        }
        
        SimpleAdapter saItem = new SimpleAdapter(this,
                  meumList, //数据源
                  R.layout.gridview_item, //xml实现
                  new String[]{"ItemImage","ItemText","ItemTypeid"}, //对应map的Key
                  new int[]{R.id.ItemImage,R.id.ItemText,R.id.Itemtypeid});  //对应R的Id
 
        //添加Item到网格中
        gridview.setAdapter(saItem);
        //添加点击事件
        gridview.setOnItemClickListener(
            new OnItemClickListener()
            {
                @Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
                {
                	TextView ItemText = (TextView) arg1.findViewById(R.id.ItemText);
                	TextView ItemTypeid = (TextView) arg1.findViewById(R.id.Itemtypeid);
                	
                	SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd");
            		String addtime = dateformat1.format(new Date());
            		if(qydataisexist(""+ItemTypeid.getText(), addtime)){
            			Toast.makeText(getApplicationContext(), "今日最好已经添加！", 0).show();
            		}else{
	                    Toast.makeText(getApplicationContext(), "你选择了"+ItemText.getText()+"种子", 0).show();
	                    Intent counter4 = new Intent(GridViewChooose.this, NewRecordAdd.class);
						Bundle bundle = new Bundle(); 
						bundle.putString("typename", ""+ItemText.getText());
						bundle.putString("typeid", ""+ItemTypeid.getText());
						counter4.putExtras(bundle); 
						startActivity(counter4);
            		}
                }
            }
        );
    }
    
    OnClickListener mListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.nagivate_btn_left:
				Intent counter = new Intent(GridViewChooose.this, TodayListNewActivity.class);
				startActivity(counter);
				finish();
				break;
			}
		}
	};
    
    @Override
	protected void onResume() {
		if (mseedManager == null) {
			mseedManager = new SeedManager(this);
			mseedManager.openDataBase();
        }
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (mseedManager != null) {
			mseedManager.closeDataBase();
			mseedManager = null;
        }
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mseedManager != null) {
			mseedManager.closeDataBase();
			mseedManager = null;
        }
		super.onPause();
	}
	
	public boolean qydataisexist(String typeid, String addtime){
		boolean isexist = false;
		if(Integer.parseInt(typeid)==0){
			Integer flagisqy = mseedManager.findZuiHaoCount(addtime);
			if(flagisqy>0){
				isexist = true;
			}else{
				isexist = false;
			}
		}else{
			isexist = false;
		}
		return isexist;
	}

	/**
	 * 捕捉back
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent counter1 = new Intent(GridViewChooose.this, HomeActivity.class);
			startActivity(counter1);
			finish();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
